package com.ruoyi.basic.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.basic.domain.BasicFamily;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.service.IBasicFamilyService;
import com.ruoyi.basic.service.IBasicUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 家庭成员Controller
 * 
 * @author ruoyi
 * @date 2021-08-20
 */
@Controller
@RequestMapping("/basic/family")
public class BasicFamilyController extends BaseController
{
    private String prefix = "basic/family";

    @Autowired
    private IBasicFamilyService basicFamilyService;

    @Autowired
    private IBasicUserService basicUserService;

    @RequiresPermissions("basic:family:view")
    @GetMapping()
    public String family(ModelMap mmap)
    {
        if(ShiroUtils.getSysUser().isAdmin() || ShiroUtils.getSubject().hasRole("administrator")){
            mmap.put("showFlag", true);
        }else{
            mmap.put("showFlag", false);
        }
        return prefix + "/family";
    }

    /**
     * 查询家庭成员列表
     */
    @RequiresPermissions("basic:family:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasicFamily basicFamily)
    {
        startPage();
        if(!ShiroUtils.getSysUser().isAdmin() && !ShiroUtils.getSubject().hasRole("administrator")){
            basicFamily.setIdCard(ShiroUtils.getLoginName());
        }
        List<BasicFamily> list = basicFamilyService.selectBasicFamilyList(basicFamily);
        return getDataTable(list);
    }

    /**
     * 导出员工家庭成员
     */
    @RequiresPermissions("basic:family:export")
    @Log(title = "员工家庭成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasicFamily basicFamily)
    {
        List<BasicFamily> list = basicFamilyService.selectBasicFamilyList(basicFamily);
        ExcelUtil<BasicFamily> util = new ExcelUtil<BasicFamily>(BasicFamily.class);
        return util.exportExcel(list, "员工家庭成员信息数据");
    }

    @Log(title = "员工家庭成员", businessType = BusinessType.IMPORT)
    @RequiresPermissions("basic:family:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasicFamily> util = new ExcelUtil<BasicFamily>(BasicFamily.class);
        List<BasicFamily> familyList = util.importExcel(file.getInputStream());
        String message = basicFamilyService.importFamily(familyList, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("basic:family:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BasicFamily> util = new ExcelUtil<BasicFamily>(BasicFamily.class);
        return util.importTemplateExcel("员工家庭成员信息");
    }

    /**
     * 新增家庭成员
     */
    @GetMapping("/add/{userId}")
    public String add(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        BasicUser user = basicUserService.selectBasicUserById(userId);
        mmap.put("user", user == null ? new BasicUser() : user);
        return prefix + "/add";
    }

    /**
     * 新增保存家庭成员
     */
    @RequiresPermissions("basic:family:add")
    @Log(title = "家庭成员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasicFamily basicFamily)
    {
        basicFamily.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(basicFamilyService.insertBasicFamily(basicFamily));
    }

    /**
     * 修改家庭成员
     */
    @GetMapping("/edit/{familyId}")
    public String edit(@PathVariable("familyId") Long familyId, ModelMap mmap)
    {
        BasicFamily basicFamily = basicFamilyService.selectBasicFamilyByFamilyId(familyId);
        mmap.put("basicFamily", basicFamily);
        return prefix + "/edit";
    }

    /**
     * 修改保存家庭成员
     */
    @RequiresPermissions("basic:family:edit")
    @Log(title = "家庭成员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasicFamily basicFamily)
    {
        basicFamily.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(basicFamilyService.updateBasicFamily(basicFamily));
    }

    /**
     * 删除家庭成员
     */
    @RequiresPermissions("basic:family:remove")
    @Log(title = "家庭成员", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basicFamilyService.deleteBasicFamilyByFamilyIds(ids));
    }

    /**
     * 校验家庭成员姓名
     */
    @PostMapping("/checkFamilyNameUnique")
    @ResponseBody
    public String checkFamilyNameUnique(BasicFamily family)
    {
        return basicFamilyService.checkFamilyNameUnique(family);
    }
}
